#' JIP-test Calculation 
#'
#' @param sample_data Data frame from read_induction() (contains SOURCE, MILLI_SEC, DC, FLUOR)
#' @param use_PAM Logical: TRUE = use PAM (FLUOR column), FALSE = use DC (default)
#' @return Data frame with JIP-test parameters
#' @keywords internal
jip_comp = function(sample_data, use_PAM = FALSE) {
  # Validate input data
  required_cols = c("SOURCE", "MILLI_SEC", "DC", "FLUOR")
  if (!all(required_cols %in% colnames(sample_data))) {
    stop(paste("sample_data must contain columns:", paste(required_cols, collapse = ", ")))
  }
  
  # Select signal (DC or PAM)
  if (use_PAM) {
    signal = sample_data$FLUOR
  } else {
    signal = sample_data$DC
  }
  time_ms = sample_data$MILLI_SEC
  source_name = unique(sample_data$SOURCE)
  
  # --------------------------
  # Base R: Order by time_ms 
  # --------------------------
  order_idx = order(time_ms)  # Get indices to sort time_ms in ascending order
  time_ms_sorted = time_ms[order_idx]
  signal_sorted = signal[order_idx]
  
  # --------------------------
  # JIP-test Calculations (unchanged—only sorting is updated)
  # --------------------------
  # Fo (minimum fluorescence, ~0.01-0.1 ms)
  fo_idx = which.min(abs(time_ms_sorted - 0.05))  # Closest to 0.05 ms
  Fo = signal_sorted[fo_idx]
  
  # Fm (maximum fluorescence)
  Fm = max(signal_sorted)
  fm_idx = which.max(signal_sorted)
  
  # Fv = Fm - Fo
  Fv = Fm - Fo
  if (Fv < 0) warning(paste("Fv < 0 for sample", source_name, "- Check signal quality"))
  
  # φPo (maximum quantum yield: Fv/Fm)
  phi_Po = if (Fm != 0) Fv / Fm else NA
  
  # J-step (2 ms) and I-step (30 ms)
  j_idx = which.min(abs(time_ms_sorted - 2))
  FJ = signal_sorted[j_idx]
  i_idx = which.min(abs(time_ms_sorted - 30))
  FI = signal_sorted[i_idx]
  
  # dV/dt0 (initial slope of fluorescence rise)
  dt = diff(time_ms_sorted[1:5])  # Time difference for first 5 points
  dF = diff(signal_sorted[1:5])  # Fluorescence difference
  dVdt0 = if (length(dt) > 0 && Fo != Fm) mean(dF/dt) / (Fm - Fo) else NA
  
  # PI_ABS (Performance Index: ABS/RC * φPo / (1 - φPo) * ψEo / (1 - ψEo))
  # ψEo (probability of electron transport: (Fm - FJ)/(Fm - Fo))
  psi_Eo = if (Fm - Fo != 0) (Fm - FJ) / (Fm - Fo) else NA
  # ABS/RC (absorption per reaction center: approximated by Fm)
  ABS_RC = Fm
  # PI_ABS calculation
  PI_ABS = if (!is.na(phi_Po) && !is.na(psi_Eo) && phi_Po != 1 && psi_Eo != 1) {
    ABS_RC * (phi_Po / (1 - phi_Po)) * (psi_Eo / (1 - psi_Eo))
  } else {
    NA
  }
  
  # PI_total (extended PI: PI_ABS * (1 - FI/Fm) / (FI/Fm))
  PI_total = if (!is.na(PI_ABS) && FI != 0 && Fm != FI) {
    PI_ABS * ((1 - FI/Fm) / (FI/Fm))
  } else {
    NA
  }
  
  # --------------------------
  # Return parameters (base R data frame)
  # --------------------------
  data.frame(
    SOURCE = source_name,
    Fo = Fo,
    Fm = Fm,
    Fv = Fv,
    phi_Po = round(phi_Po, 4),
    FJ = FJ,
    FI = FI,
    psi_Eo = round(psi_Eo, 4),
    dVdt0 = round(dVdt0, 6),
    ABS_RC = round(ABS_RC, 4),
    PI_ABS = round(PI_ABS, 4),
    PI_total = round(PI_total, 4),
    stringsAsFactors = FALSE
  )
}

